package com.gujia.rabbitmq.work;

import com.gujia.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class WorkConsumers1 {

    private static final String QUEUE_NAME="test_work_queue";

    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtils.getConnction();

        Channel channel = connection.createChannel();

        //声明队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);


        DefaultConsumer consumer = new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body,"utf-8");

                System.out.println("[1] WorkConsumers1 MSG:"+msg);

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("[1] done!");
                }
            }
        };

        channel.basicConsume(QUEUE_NAME,true,consumer);
    }
}
